import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.FileReader;
import java.io.IOException;
import JavaBean.User;
@WebServlet("/upload")
@MultipartConfig(location = "E:\\工程\\test1\\src\\main\\webapp\\picture")// location改为本地的绝对路径
public class UploadServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        Part part = request.getPart("file");
        User user =(User)request.getSession().getAttribute("user");
        String name = user.getSno();
        String fileName = name+".jpg";
        part.write(fileName);
        user.setImg("picture/"+ fileName);
        response.sendRedirect("/UserPage.jsp");
    }
}
